﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using AutoUpdaterDotNET;
using DevComponents.DotNetBar;
using Newtonsoft.Json;

namespace AppCash
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            //登录
            if (tbPwd.Text == "" || cbUserid.Text == "")
            {
                MessageBoxEx.Show("请输入登录口令!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            ZhiKeDuoServer.BLL.OperInfo bOper = new ZhiKeDuoServer.BLL.OperInfo();

            string json = System.IO.File.ReadAllText(System.Environment.CurrentDirectory + "/keyconfig");
            var rlt = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
            ZhiKeDuoServer.Model.GlobalsInfo.PrinterIPAddress = rlt["ipaddr"];
            if (System.IO.File.Exists(System.Environment.CurrentDirectory + "/banci"))
            {
                String banci = System.IO.File.ReadAllText(System.Environment.CurrentDirectory + "/banci");

                ZhiKeDuoServer.Model.GlobalsInfo.banci = Convert.ToInt32(banci);
            }
            else {
                ZhiKeDuoServer.Model.GlobalsInfo.banci = 0;
            }
            if (bOper.CheckUser(cbUserid.Text, tbPwd.Text))
            {
                ZhiKeDuoServer.Model.GlobalsInfo.UserName = cbUserid.Text;

                frmMain frm = new frmMain();

                frm.Show();
                this.Visible = false;
            }
            else
            {
                MessageBoxEx.Show("您输入的口令不正确!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {

            //初始化界面信息
            //提取用户列表

            ZhiKeDuoServer.BLL.OperInfo mOper = new ZhiKeDuoServer.BLL.OperInfo();
            var ds = mOper.GetListByPage("","Id asc",100,0);
            cbUserid.ValueMember = "Code";
            cbUserid.DisplayMember = "Code";
            cbUserid.DataSource = ds;

            //提取配置信息
            ZhiKeDuoServer.BLL.ShopInfo bShop = new ZhiKeDuoServer.BLL.ShopInfo();
            ZhiKeDuoServer.Model.ShopInfo mShop = new ZhiKeDuoServer.Model.ShopInfo();
            mShop = bShop.GetModel(1);
            ZhiKeDuoServer.Model.GlobalsInfo.Shop = mShop;
            this.Text = this.Text + " --【" + mShop.Name + "】";
            checkUpdate();

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        public void checkUpdate()
        {
            AutoUpdater.Start("http://www.zhikeduo.cn/huiaitang2.xml");
        }


        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Escape:
                    Application.Exit();
                    return true;
                case Keys.Enter:
                    btnLogin_Click(null, null);
                    return true;
                default:
                    return base.ProcessCmdKey(ref msg, keyData);
            }
        }
    }
}
